package com.ykt.mall.ctrl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.ykt.mall.content.Content;
import com.ykt.model.Customer;
import com.ykt.model.CustomerAddress;
import com.ykt.model.Shop;

@ControllerBind(controllerKey="/address")//viewPath="/template"
public class AddressCtrl extends Controller {
	
	public void view(){
		Integer id = this.getParaToInt("id");
		
		Customer loginCustomer = getSessionAttr(Content.SESSION_LOGIN_USER);
		this.setAttr("addr", CustomerAddress.dao.findById(id));
		this.setAttr("addrList", CustomerAddress.dao.find("select * from customer_address where def_addr='1' and customer_id=? order by id desc ",loginCustomer.getInt("id") ));
	}
	
	public void save() {
		String msg = "";
		try {
			Customer loginCustomer = getSessionAttr(Content.SESSION_LOGIN_USER);
			CustomerAddress m = this.getModel(CustomerAddress.class);
			Integer id = m.getInt("id");
			Date now=new Date();
			if (id != null && id != 0) {
				msg="修改成功！";
				m.set("update_time",now);//修改时间
				m.update();
			} else {
				msg="保存成功！";
				m.set("customer_id", loginCustomer.getInt("id"));
				m.set("create_time",now);//创建时间
				m.set("update_time",now);
				m.save();
			}
			
			id = m.getInt("id");
			//rendJson_(true,msg, id,new Random().nextLong());
		} catch (Exception e) {
			//log.error("保存异常", e);
			//rendJson_(false, "保存异常！");
			e.printStackTrace();
		}
		redirect("/address/view");
	}
	
	public void delete(){
		Integer id = this.getParaToInt("id");
		CustomerAddress.dao.deleteById(id);
		this.setAttr("addrList", CustomerAddress.dao.find("select * from customer_address order by id desc"));
		this.renderJson("Msg", "Ok");
	}
	
	public void edit(){
		Integer id = this.getParaToInt("id");
		CustomerAddress c = CustomerAddress.dao.findById(id);
		c.set("default", "1");
		c.update();
		this.renderJson("Msg", "Ok");
	}
	
	public void saveFrompay(){
		Customer loginCustomer = getSessionAttr(Content.SESSION_LOGIN_USER);
		Shop curShop = getSessionAttr(Content.SESSION_SHOP);
		
		Date now=new Date();
		String addr_name = this.getPara("addr-name");
		String provice = this.getPara("sltStateProvince");
		String sltCity = this.getPara("sltCity");
		String sltCityArea = this.getPara("sltCityArea");
//		System.out.println("========================" + provice + "=====" + sltCity + "===========" + sltCityArea);
		String addr_detail = this.getPara("addr-detail");
		String addr_phone = this.getPara("addr-phone");
		CustomerAddress m =  new CustomerAddress();
		
		m.set("shop_id", curShop.getInt("id"));
		m.set("customer_id", loginCustomer.getInt("id"));
		
		m.set("consignee", addr_name);
		m.set("prov", provice);
		m.set("city", sltCity);
		m.set("area", sltCityArea);
		m.set("address", addr_detail);
		m.set("phone", addr_phone);
		m.set("create_time", now);
		m.set("create_time", now);
		m.set("def_addr", "1");
		m.save();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("md", m);
		map.put("Msg", "Ok");
		renderJson(map);
	}
	
}
